
#include <stdlib.h>
#include <event.h>

struct event_base *__nk_ev_base__;

void nk_ev_init(void)
{
	__nk_ev_base__ = event_base_new();
}

int nk_ev_set(int fd, short events, void (*cb)(int, short, void*), void *arg)
{
	struct event *ev;

	if (!__nk_ev_base__) {
		nk_ev_init();
	}

	ev = (struct event*)malloc(sizeof(*ev));
	if (!ev) {
		return -1;
	}

	event_set(ev, fd, events, cb, arg);
	event_base_set(__nk_ev_base__, ev);
	event_add(ev, NULL);

	return 0;
}

void nk_ev_dispatch(void)
{
	event_base_dispatch(__nk_ev_base__);
}


